package org.jeecg.modules.qy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.qy.entity.QyAccount;
import org.jeecg.modules.qy.entity.QyCommodity;
import org.jeecg.modules.qy.entity.QyCommodityAttribute;
import org.jeecg.modules.qy.entity.vo.SourceDataVo;
import org.jeecg.modules.qy.entity.vo.VisitUserVo;
import org.jeecg.modules.qy.service.IQyAccountService;
import org.jeecg.modules.qy.service.IQyCommodityAttributeService;
import org.jeecg.modules.qy.service.IQyCommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 商品表
 * @Author: jeecg-boot
 * @Date:   2021-03-10
 * @Version: V1.0
 */
@Api(tags="商品表")
@RestController
@RequestMapping("/qy/qyCommodity")
@Slf4j
public class QyCommodityController extends JeecgController<QyCommodity, IQyCommodityService> {
	@Autowired
	private IQyCommodityService qyCommodityService;

	/**
	 *商品属性
	 */
	@Autowired
	private IQyCommodityAttributeService qyCommodityAttributeService;

	
	/**
	 * 分页列表查询
	 *
	 * @param qyCommodity
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商品表-分页列表查询")
	@ApiOperation(value="商品表-分页列表查询", notes="商品表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(QyCommodity qyCommodity,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<QyCommodity> queryWrapper = QueryGenerator.initQueryWrapper(qyCommodity, req.getParameterMap());
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		queryWrapper.eq("company_id",loginUser.getCompanyId());
		queryWrapper.eq("commodity_status",1);
		queryWrapper.orderByDesc("create_time");
		Page<QyCommodity> page = new Page<QyCommodity>(pageNo, pageSize);
		IPage<QyCommodity> pageList = qyCommodityService.page(page, queryWrapper);
		if(CollectionUtils.isNotEmpty(pageList.getRecords())){
			for(QyCommodity qty:pageList.getRecords()){
				List<QyCommodityAttribute> buteList = qyCommodityAttributeService.list(new QueryWrapper<QyCommodityAttribute>()
						.eq("commodity_id",qty.getId()).orderByAsc("sort"));
				qty.setCommodityAttributeList(buteList);
			}
		}
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param qyCommodity
	 * @return
	 */
	@AutoLog(value = "商品表-添加")
	@ApiOperation(value="商品表-添加", notes="商品表-添加")
	@RequiresPermissions("qyCommodity:add")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody QyCommodity qyCommodity) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		qyCommodityService.saveComodity(qyCommodity,loginUser.getCompanyId());
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param qyCommodity
	 * @return
	 */
	@AutoLog(value = "商品表-编辑")
	@ApiOperation(value="商品表-编辑", notes="商品表-编辑")
	@RequiresPermissions("qyCommodity:edit")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody QyCommodity qyCommodity) {
		qyCommodityService.updateComodity(qyCommodity);
		return Result.OK(qyCommodity.getId());
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	/*@AutoLog(value = "商品表-通过id删除")
	@ApiOperation(value="商品表-通过id删除", notes="商品表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		qyCommodityService.removeById(id);
		return Result.OK("删除成功!");
	}*/
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
/*	@AutoLog(value = "商品表-批量删除")
	@ApiOperation(value="商品表-批量删除", notes="商品表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.qyCommodityService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}*/
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商品表-通过id查询")
	@ApiOperation(value="商品表-通过id查询", notes="商品表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=false) String id) {
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QyCommodity qyCommodity = qyCommodityService.getComodity(id,loginUser.getCompanyId());
		return Result.OK(qyCommodity);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param qyCommodity
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, QyCommodity qyCommodity) {
        return super.exportXls(request, qyCommodity, QyCommodity.class, "商品表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, QyCommodity.class);
    }

	/**
	 * 查询已完成的商品列表
	 *
	 * @return
	 */
	@AutoLog(value = "商品表-通过查询已完成的商品列表")
	@ApiOperation(value="商品表-通过查询已完成的商品列表", notes="商品表-通过查询已完成的商品列表")
	@GetMapping(value = "/findCommodityList")
    public Result<?>  findCommodityList(){
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<QyCommodity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("company_id",loginUser.getCompanyId());
		queryWrapper.eq("commodity_status",1);
		queryWrapper.orderByDesc("create_time");
		return Result.OK(qyCommodityService.list(queryWrapper));
	}


	/**
	 * 查询商品溯源数据列表
	 *
	 * @return
	 */
	@AutoLog(value = "查询商品溯源数据列表")
	@ApiOperation(value="查询商品溯源数据列表", notes="查询商品溯源数据列表")
	@GetMapping(value = "/findSourceDataList")
	public Result<?> findSourceDataList(SourceDataVo dataVo,
										@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										@RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		dataVo.setCompanyId(loginUser.getCompanyId());
		return Result.OK(qyCommodityService.findSourceDataList(dataVo,pageNo,pageSize));
	}



	/**
	 * 查询用户访问列表
	 *
	 * @return
	 */
	@AutoLog(value = "查询用户访问列表")
	@ApiOperation(value="查询用户访问列表", notes="查询用户访问列表")
	@GetMapping(value = "/findVisitUserList")
	public Result<?> findVisitUserList(VisitUserVo vo,
									   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
		return Result.OK(qyCommodityService.findVisitUserList(vo,pageNo,pageSize));
	}

	/**
	 * 查询用户访问特征数据
	 *
	 * @return
	 */
	@AutoLog(value = "查询用户访问特征数据")
	@ApiOperation(value="查询用户访问特征数据", notes="查询用户访问特征数据")
	@GetMapping(value = "/findUsersSpecils")
	public Result<?> findUsersSpecils(@RequestParam(name="commodityId") String commodityId,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="100") Integer pageSize){
		return qyCommodityService.findUsersSpecils(commodityId,pageNo,pageSize);
	}

	/**
	 * 查询用户访问省级数据
	 *
	 * @return
	 */
	@AutoLog(value = "查询用户访问省级数据")
	@ApiOperation(value="查询用户访问省级数据", notes="查询用户访问省级数据")
	@GetMapping(value = "/findUsersProvince")
	public Result<?> findUsersProvince(@RequestParam(name="commodityId") String commodityId,
									   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									   @RequestParam(name="pageSize", defaultValue="100") Integer pageSize){
		return qyCommodityService.findUsersProvince(commodityId,pageNo,pageSize);
	}

	/**
	 * 查询用户访问省市数据
	 *
	 * @return
	 */
	@AutoLog(value = "查询用户访问省市数据")
	@ApiOperation(value="查询用户访问省市数据", notes="查询用户访问省市数据")
	@GetMapping(value = "/findUsersProvinceCity")
	public Result<?> findUsersProvinceCity(@RequestParam(name="commodityId") String commodityId,
										   @RequestParam(name = "province")String province,
										   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										   @RequestParam(name="pageSize", defaultValue="100") Integer pageSize){
		return qyCommodityService.findUsersProvinceCity(commodityId,province,pageNo,pageSize);
	}

	/**
	 * 查询用户访问设备数据
	 *
	 * @return
	 */
	@AutoLog(value = "查询用户访问设备数据")
	@ApiOperation(value="查询用户访问设备数据", notes="查询用户访问设备数据")
	@GetMapping(value = "/findUsersVisitDevice")
	public Result<?> findUsersVisitDevice(@RequestParam(name="commodityId") String commodityId,
										  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
										  @RequestParam(name="pageSize", defaultValue="100") Integer pageSize){
		return qyCommodityService.findUsersVisitDevice(commodityId,pageNo,pageSize);
	}
}
